import cv2
import numpy as np
from matplotlib import pyplot as plt
import time

from PIL import ImageGrab

import cv2
import numpy as np
# from matplotlib import pyplot as plt
import autopy3 as autopy
template = cv2.imread('凶暴野兽闪光.png', 0)
template = cv2.imread('狂暴野兽.png', 0)
w, h = template.shape[::-1]
import time


# im0 =ImageGrab.grab((0, 0, 2880, 1800))
# im0.show()
# im0 =ImageGrab.grab((0, 1500, 2880, 1800))
# im0.show()


i=0

while 1:
    time1 = time.time()
    # time.sleep(2)
    # img_rgb = np.array(ImageGrab.grab().convert('RGB'))
    img_rgb = cv2.imread('火法.png')
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)



    res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)# 大图 ，目标 ，模式
    threshold = 0.6  #阈值
    loc = np.where(res >= threshold)
    for pt in zip(*loc[::-1]):
        cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), )
        i+=1
        print(i)
        # autopy.mouse.move(int(pt[0]+w), int(pt[1]+h))
        #
        # print(h )
        # autopy.key.toggle(0x15, True, )
        # autopy.key.toggle(0x15, False, )
    # cv2.imwrite('res.png', img_rgb)

    time2 = time.time()
    print(time2 - time1)

